package TeacherEvaluation.com.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Admin;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.entity.Student;
import TeacherEvaluation.com.entity.Teacher;
import TeacherEvaluation.com.service.loginService;

/**
 * 登录所用控制器
 * 
 * @author azhao 2019/09/16
 */
@Controller
public class loginController {
	
	@Autowired
	private loginService loginservice;
	
	// 个人信息提交登录
	//@RequestMapping(value=("/login_submit"),method=RequestMethod.GET)
	@RequestMapping("/login_submit")
	@ResponseBody
	public ResultMsg Login_submit(String studentID, String password, String type,HttpServletRequest request) {
		String username;
		username = studentID;
		// 获取session
		boolean isLogin = false;
		HttpSession session=request.getSession();
		
		if (type.equals("0")) {
			// 判断是否存在这个用户
			isLogin = loginservice.getAdminByNameAndPass(username);
			if (isLogin == true) {
				// 获取此用户的信息
				Admin admin = loginservice.getAdminInfo(username, password);
				if (admin!=null) {
					// 密码相等，登录成功，保存到session
					session.setAttribute("user", admin);
					//返回7表示管理员登录
					return new ResultMsg(7, "登陆成功!");

				} else {
					return new ResultMsg(2, "密码错误!");
				}

			} else {
				return new ResultMsg(1, "不存在此用户");
			}

		} else if (type.equals("1")) {
			isLogin = loginservice.getTeacherByNameAndPass(username);
			if (isLogin == true) {
				Teacher teacher = loginservice.getTeacherInfo(username, password);
				if (teacher!=null) {
					session.setAttribute("user", teacher);
					//返回8表示老师登陆
					return new ResultMsg(8, "登陆成功!");

				} else {
					return new ResultMsg(2, "密码错误!");
				}

			} else {
				return new ResultMsg(1, "不存在此用户");
			}

		} else {
			isLogin = loginservice.getStudentByNameAndPass(username);
			if (isLogin == true) {
				Student student = loginservice.getStudentInfo(username, password);
				if (student!=null) {
					session.setAttribute("user", student);
					//返回9表示学生登陆
					return new ResultMsg(9, "登陆成功!");

				} else {
					return new ResultMsg(2, "密码错误!");
				}

			} else {
				return new ResultMsg(1, "不存在此用户");
			}

		}

	}
	
	// 访问登录页面
		@RequestMapping("/login")
		public ModelAndView Login(HttpServletRequest request) {
			ModelAndView mv = new ModelAndView("redirect:/index");
			ModelAndView mv2 = new ModelAndView("login");
			HttpSession session=request.getSession();

			if(session.getAttribute("user")!=null) {
				return mv;
			}else {
				return mv2;
			}
			
			
		}

}
